//
//  PopCatPageController.m
//  MindBox
//
//  Created by MS on 16-1-8.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "PopCatPageController.h"
#import "GoodsController.h"
#import "RaidersDetailController.h"
#import "GoodsDetailController.h"
#import "GoodsListController.h"
#import "PopCatPageDataTool.h"
#import "GiftPageDataTool.h"
#import "PopCatView.h"
@interface PopCatPageController ()<PopCatViewDelegate>


@property (nonatomic,weak) PopCatView * popCatView;
@end

@implementation PopCatPageController
-(NSMutableArray *)itemGoodsArray
{
    if (_itemGoodsArray==nil) {
        _itemGoodsArray = [NSMutableArray array];
    }
    return _itemGoodsArray;
}

-(PopCatView *)popCatView
{
    if (_popCatView==nil) {
        PopCatView * view = [[PopCatView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height-64)];
        _popCatView = view;
        _popCatView.popViewdelegate = self;
        [self.view addSubview:view];
    }
    return _popCatView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavBar];
    
   
    [self loadHeaderPageData];
    [self loadPageItemGoods_Data];
    
    
    [self addFootRefresh];
    [self addHeaderRefresh];
}

-(void)loadHeaderPageData
{
    [PopCatPageDataTool popCatPageDataWithCat_id:self.cat_id Success:^(PopCatPage *data) {
        [_popCatView.collectionView.mj_header endRefreshing];

        self.popCatView.popCatData = data;
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)loadPageItemGoods_Data
{
    [GiftPageDataTool giftPage_ItemGoods_DataWithCat_id:self.cat_id seach_code:self.gos_id pages:++self.pages Success:^(NSArray *data) {
        [_popCatView.collectionView.mj_footer endRefreshing];
        [_popCatView.collectionView.mj_header endRefreshing];

        if (data.count) {
            [self.itemGoodsArray addObjectsFromArray:data];
        }
        
        self.popCatView.gift_ItemGoods = self.itemGoodsArray;
        
    } failure:^(NSError *error) {
        
    }];
    
}


-(void)addFootRefresh
{
    __weak typeof(self) weakSelf = self;
    
    self.popCatView.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadPageItemGoods_Data];
    }];
    
}

-(void)addHeaderRefresh
{
    __weak typeof(self) weakSelf = self;
    
    self.popCatView.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pages = 0;
        [weakSelf.itemGoodsArray removeAllObjects];
        [weakSelf loadPageItemGoods_Data];
    }];
}

-(void)setNavBar
{
    MyNavigationItem * leftItem = [MyNavigationItem navigationItemWithImage:@"left_go" andHighlightImage:@"left_go" andTag:0 andTitle:nil addTarget:self action:@selector(leftBtnClick)];
    
    MyNavigationItem * rightItem =[MyNavigationItem navigationItemWithImage:@"threedot" andHighlightImage:@"threedot" andTag:0 andTitle:nil addTarget:self action:@selector(rightClick)];
    
    MyNavigationBar * navBar = [[MyNavigationBar alloc]initWithFrame:CGRectMake(0, 20, self.view.width, 44)];
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, navBar.frame.size.height-0.5, navBar.frame.size.width, 0.5)];
    lineView.backgroundColor =[UIColor grayColor];
    [navBar addSubview:lineView];
    
    [navBar setNavigationBarWithBgImageName:@"无" andTitle:self.title andTitleView:nil  andLeftItems:@[leftItem] andRightItems:@[rightItem]];
    [self.view addSubview:navBar];
    
}

-(void)leftBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)rightClick{
    
    
}

#pragma mark PopView代理方法
-(void)popCatSelectViewDidClick:(PopCatView *)popCatView selectItem:(PopCat_Gift_Cat_Id *)cat_id index:(NSInteger)index
{
    
    self.gos_id = cat_id.gos_id;
    [self.itemGoodsArray removeAllObjects];
    self.pages = 0;
    [self loadPageItemGoods_Data];
}

-(void)popAdsViewDidClick:(PopCatView *)popCatView banner:(PopCat_Banner *)banner index:(NSInteger)index
{
    GoodsListController * vc = [[GoodsListController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.titleName = banner.name;
    if ([banner.table_name isEqualToString:@"goods_category"]) {
        vc.category_id = banner.table_id;
        [self.navigationController pushViewController:vc animated:YES];

    }
    else if ([banner.table_name isEqualToString:@"goods_label"]){
        vc.label_id = banner.table_id;
        [self.navigationController pushViewController:vc animated:YES];

    }
    else if ([banner.table_name isEqualToString:@"key_search"]){
        vc.name_id = banner.name;
        [self.navigationController pushViewController:vc animated:YES];
    }

    else if ([banner.table_name isEqualToString:@"activity"]){
        RaidersDetailController * raiderVc = [[RaidersDetailController alloc] init];
        raiderVc.urlStr = banner.name;
        
        [self.navigationController pushViewController:raiderVc animated:YES];
    }
}

-(void)popSndViewDidClick:(PopCatView *)popCatView snd_banner:(Snd_Banner *)snd_banner index:(NSInteger)index
{
    GoodsListController * vc = [[GoodsListController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.label_id = snd_banner.table_id;
    vc.titleName = snd_banner.name;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)popGoodsViewDidClick:(PopCatView *)popCatView goods:(PopCat_Goods *)goods index:(NSInteger)index
{
    GoodsController * vc  = [[GoodsController alloc] init];
    vc.good_id = goods.goods_id;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)popColletionViewDidClick:(PopCatView *)popCatView goods:(Gift_ItemGoods *)itemgoods index:(NSInteger)index
{
    GoodsController * vc  = [[GoodsController alloc] init];
    vc.good_id = itemgoods.goods_id;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)dealloc
{
//    NSLog(@"PopCatPageController销毁");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
